package com.lnsoft.cac.server.handle.codec.frameMessage;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class Util {
    //  检查监测点ID是否可用
    public static boolean measurePointIDIsAvailable(int Measure_Point_ID) {
        if (Measure_Point_ID < 0 || Measure_Point_ID > 255) {
            return false;
        }
        return true;
    }

    //  检查检测数据条目数是否可用
    public static boolean measureDataNumIsAvailable(int MeasureData_Num) {
        if (MeasureData_Num < 0 || MeasureData_Num > 255) {
            return false;
        }
        return true;
    }

    //  检查采集时间是否可用
    public static boolean statTimeIsAvailable(int StatTime) {
        if (StatTime <= 0) {
            return false;
        }
        return true;
    }

    //  电压合格率、电压越上限率数据格式转化 0 <= rateValue <= 0
    public static byte[] rateValueToBytes(String rateValue) throws Exception{
        byte[] rateBytes = new byte[3];
        BigDecimal t = new BigDecimal(rateValue);
        if (t.compareTo(BigDecimal.valueOf(1)) > 0) {
            throw new Exception("rateValue is err.");
        }
        BigDecimal tt = t.setScale(5, BigDecimal.ROUND_HALF_UP);
        BigDecimal t1 = tt.multiply(BigDecimal.valueOf(100));
        int t11 = t1.divide(BigDecimal.valueOf(100)).intValue();
        BigDecimal t2 = t1.subtract(BigDecimal.valueOf(t11 * 100));
        int t22 = t2.divide(BigDecimal.valueOf(10)).intValue();
        BigDecimal t3 = t2.subtract(BigDecimal.valueOf(t22 * 10));
        int t33 = t3.intValue();
        BigDecimal t4 = t3.subtract(BigDecimal.valueOf(t33));
        int t44 = t4.multiply(BigDecimal.valueOf(10)).intValue();
        BigDecimal t5 = t4.subtract(BigDecimal.valueOf(t44).divide(BigDecimal.valueOf(10)));
        int t55 = t5.multiply(BigDecimal.valueOf(100)).intValue();
        BigDecimal t6 = t5.subtract(BigDecimal.valueOf(t55).divide(BigDecimal.valueOf(100)));
        int t66 = t6.multiply(BigDecimal.valueOf(1000)).intValue();
        rateBytes[2] = (byte) (((t11 << 4) | t22) & 0xFF);
        rateBytes[1] = (byte) (((t33 << 4) | t44) & 0xFF);
        rateBytes[0] = (byte) (((t55 << 4) | t66) & 0xFF);
        return rateBytes;
    }

    //  电压值数据格式转化
    public static byte[] voltageValueToBytes(String voltageValue) {
        byte[] voltage = new byte[4];
        BigDecimal t1 = new BigDecimal(voltageValue);
        int t11 = t1.divide(BigDecimal.valueOf(100000)).intValue();
        BigDecimal t2 = t1.subtract(BigDecimal.valueOf(t11 * 100000));
        int t22 = t2.divide(BigDecimal.valueOf(10000)).intValue();
        BigDecimal t3 = t2.subtract(BigDecimal.valueOf(t22 * 10000));
        int t33 = t3.divide(BigDecimal.valueOf(1000)).intValue();
        BigDecimal t4 = t3.subtract(BigDecimal.valueOf(t33 * 1000));
        int t44 = t4.divide(BigDecimal.valueOf(100)).intValue();
        BigDecimal t5 = t4.subtract(BigDecimal.valueOf(t44 * 100));
        int t55 = t5.divide(BigDecimal.valueOf(10)).intValue();
        BigDecimal t6 = t5.subtract(BigDecimal.valueOf(t55 * 10));
        int t66 = t6.intValue();
        BigDecimal t7 = t6.subtract(BigDecimal.valueOf(t66));
        int t77 = t7.multiply(BigDecimal.valueOf(10)).intValue();
        BigDecimal t8 = t7.subtract(BigDecimal.valueOf(t77).divide(BigDecimal.valueOf(10)));
        int t88 = t8.multiply(BigDecimal.valueOf(100)).intValue();

        voltage[3] = (byte) (((t11 << 4) | t22) & 0xFF);
        voltage[2] = (byte) (((t33 << 4) | t44) & 0xFF);
        voltage[1] = (byte) (((t55 << 4) | t66) & 0xFF);
        voltage[0] = (byte) (((t77 << 4) | t88) & 0xFF);
        return voltage;
    }
    //  电压值字节转为十进制字符串
    public static String voltageBytesToValue(byte[] vol) throws Exception{
        if (vol == null || vol.length != 4) {
            throw new Exception("vol is err.");
        }
        BigDecimal v = new BigDecimal("0");
        BigDecimal b8 = v.add(BigDecimal.valueOf(((vol[3] >> 4) & 0xFF) * 100000));
        BigDecimal b7 = b8.add(BigDecimal.valueOf((vol[3] & 0x0F) * 10000));
        BigDecimal b6 = b7.add(BigDecimal.valueOf(((vol[2] >> 4) & 0xFF) * 1000));
        BigDecimal b5 = b6.add(BigDecimal.valueOf((vol[2] & 0x0F) * 100));
        BigDecimal b4 = b5.add(BigDecimal.valueOf(((vol[1] >> 4) & 0xFF) * 10));
        BigDecimal b3 = b4.add(BigDecimal.valueOf(vol[1] & 0x0F));
        BigDecimal b2 = b3.add(BigDecimal.valueOf((vol[0] >> 4) & 0xFF).multiply(new BigDecimal("0.1")));
        BigDecimal b1 = b2.add(BigDecimal.valueOf(vol[0] & 0x0F).multiply(new BigDecimal("0.01")));
//        b1.setScale(3, BigDecimal.ROUND_HALF_UP);
        return b1.toString();
    }

    //  电压合格率字节数组转为十进制字符串 0 <= 百分率 <= 1
    public static String rateBytesToValue(byte[] rateBytes) throws Exception{
        if (rateBytes == null || rateBytes.length != 3) {
            throw new Exception("rateBytes is err.");
        }
        BigDecimal v = new BigDecimal("0");

        BigDecimal b6 = v.add(BigDecimal.valueOf(((rateBytes[2] >> 4) & 0x0F) * 100));
        BigDecimal b5 = b6.add(BigDecimal.valueOf((rateBytes[2] & 0x0F) * 10));
        BigDecimal b4 = b5.add(BigDecimal.valueOf(((rateBytes[1] >> 4) & 0x0F) * 1));
        BigDecimal b3 = b4.add(BigDecimal.valueOf(rateBytes[1] & 0x0F).multiply(new BigDecimal("0.1")));
        BigDecimal b2 = b3.add(BigDecimal.valueOf((rateBytes[0] >> 4) & 0x0F).multiply(new BigDecimal("0.01")));
        BigDecimal b1 = b2.add(BigDecimal.valueOf(rateBytes[0] & 0x0F).multiply(new BigDecimal("0.001")));
        BigDecimal b0 = b1.divide(BigDecimal.valueOf(100));
        DecimalFormat df = new DecimalFormat("0.000%");
        return df.format(b0).replaceAll("%","");
    }


    // 获取日期
    public static String getDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(System.currentTimeMillis()));
    }

    //世纪秒转换日期
//    public Static
    public static String getVersion(int v) {
        StringBuilder sb = new StringBuilder();
        sb.append((v >> 24) & 0x000000FF).append(".");
        sb.append((v >> 16) & 0x000000FF).append(".");
        sb.append((v >> 8) & 0x000000FF).append(".");
        sb.append(v & 0x000000FF);
        return null;
    }

    //  字节数组转为字符串，在字节数组中，字符串以二进制0x00结束
    public static String byteArrToString(byte[] arrBytes) {
        int len = arrBytes.length;
        int ii = 0;
        for (int index = 0; index < len; index++) {
            if (arrBytes[index] == 0) {
                break;
            }
            ii = index;
        }

        byte[] cc = new byte[ii + 1];
        for (int i = 0; i < ii + 1; i++) {
            cc[i] = arrBytes[i];
        }
        return new String(cc);
    }

}

